function RongInit(params) {
    RongIMLib.RongIMClient.init(params.appKey);
    // 连接状态监听器
    RongIMClient.setConnectionStatusListener({
        onChanged: function (status) {
            switch (status) {
                case RongIMLib.ConnectionStatus.CONNECTED:
                    console.log('链接成功');
                    break;
                case RongIMLib.ConnectionStatus.CONNECTING:
                    console.log('正在链接');
                    break;
                case RongIMLib.ConnectionStatus.DISCONNECTED:
                    console.log('断开连接');
                    break;
                case RongIMLib.ConnectionStatus.KICKED_OFFLINE_BY_OTHER_CLIENT:
                    console.log('其他设备登录');
                    break;
                case RongIMLib.ConnectionStatus.DOMAIN_INCORRECT:
                    console.log('域名不正确');
                    break;
                case RongIMLib.ConnectionStatus.NETWORK_UNAVAILABLE:
                    console.log('网络不可用');
                    break;
            }
        }
    });

    // 消息监听器
    RongIMClient.setOnReceiveMessageListener({
        // 接收到的消息
        onReceived: function (message) {
            // 判断消息类型
            switch(message.messageType){
                case RongIMClient.MessageType.TextMessage:
                    var content = message.content.content;
                    var extra   = message.content.extra;
                    if (typeof extra != "object"){
                        extra = eval('(' + extra + ')');
                    }
                    onReceivedMessage(content,extra);
                    break;
                case RongIMClient.MessageType.VoiceMessage:
                    // message.content.content => 格式为 AMR 的音频 base64
                    break;
                case RongIMClient.MessageType.ImageMessage:
                    // message.content.content => 图片缩略图 base64
                    // message.content.imageUri => 原图 URL
                    break;
                case RongIMClient.MessageType.LocationMessage:
                    // message.content.latiude => 纬度
                    // message.content.longitude => 经度
                    // message.content.content => 位置图片 base64
                    break;
                case RongIMClient.MessageType.RichContentMessage:
                    // message.content.content => 文本消息内容
                    // message.content.imageUri => 图片 base64
                    // message.content.url => 原图 URL
                    break;
                case RongIMClient.MessageType.InformationNotificationMessage:
                    break;
                case RongIMClient.MessageType.ContactNotificationMessage:
                    break;
                case RongIMClient.MessageType.ProfileNotificationMessage:
                    break;
                case RongIMClient.MessageType.CommandNotificationMessage:
                    break;
                case RongIMClient.MessageType.CommandMessage:
                    break;
                case RongIMClient.MessageType.UnknownMessage:
                    break;
                default:
                // do something
            }
        }
    });

    RongIMClient.connect(params.token, {
        onSuccess: function(userId) {
            console.log('Connect successfully. ' + userId);
            var chatRoomId = params.RoomId; // 聊天室 Id
            var count = 0; // 拉取最近聊天最多 50 条
            RongIMClient.getInstance().joinChatRoom(chatRoomId, count, {
                onSuccess: function() {
                    WelcomeRoom();
                    console.log('加入聊天室成功')
                },
                onError: function(error) {
                    console.log('加入聊天室失败')
                }
            });

        },
        onTokenIncorrect: function() {
            console.log('token 无效');
        },
        onError: function(errorCode){
            var info = '';
            switch (errorCode) {
                case RongIMLib.ErrorCode.TIMEOUT:
                    info = '超时';
                    break;
                case RongIMLib.ConnectionState.UNACCEPTABLE_PAROTOCOL_VERSION:
                    info = '不可接受的协议版本';
                    break;
                case RongIMLib.ConnectionState.IDENTIFIER_REJECTED:
                    info = 'appkey不正确';
                    break;
                case RongIMLib.ConnectionState.SERVER_UNAVAILABLE:
                    info = '服务器不可用';
                    break;
            }
            console.log(info);
        }
    });
}

function SendMessage(content,extra,RoomId) {
    var msg = new RongIMLib.TextMessage({ content: content, extra:extra });
    var conversationType = RongIMLib.ConversationType.CHATROOM;
    var targetId = RoomId; // 目标 Id
    RongIMClient.getInstance().sendMessage(conversationType, targetId, msg, {
        onSuccess: function (message) {
            // message 为发送的消息对象并且包含服务器返回的消息唯一 Id 和发送消息时间戳
            console.log('发送成功: '+ message.content.content);
        },
        onError: function (errorCode, message) {
            var info = '';
            switch (errorCode) {
                case RongIMLib.ErrorCode.TIMEOUT:
                    info = '超时';
                    break;
                case RongIMLib.ErrorCode.UNKNOWN:
                    info = '未知错误';
                    break;
                case RongIMLib.ErrorCode.REJECTED_BY_BLACKLIST:
                    info = '在黑名单中，无法向对方发送消息';
                    break;
                case RongIMLib.ErrorCode.NOT_IN_DISCUSSION:
                    info = '不在讨论组中';
                    break;
                case RongIMLib.ErrorCode.NOT_IN_GROUP:
                    info = '不在群组中';
                    break;
                case RongIMLib.ErrorCode.NOT_IN_CHATROOM:
                    info = '不在聊天室中';
                    break;
            }
            console.log('发送失败: ' + info + errorCode);
        }
    });
}

/**
 * @param RoomId
 */
function getChatRoomInfo(RoomId) {
    var chatRoomId = RoomId; // 聊天室 Id
    var count = 6; // 获取聊天室人数 （范围 0-20 ）
    var order = RongIMLib.GetChatRoomType.REVERSE; // 排序方式
    RongIMClient.getInstance().getChatRoomInfo(chatRoomId, count, order, {
        onSuccess: function(chatRoom) {
            count_num(chatRoom.userTotalNums);
            count_head_ico(chatRoom.userInfos)
            // chatRoom => 聊天室信息
            // chatRoom.userInfos => 返回聊天室成员
            // chatRoom.userTotalNums => 当前聊天室总人数
        },
        onError: function(error) {
            // 获取聊天室信息失败
        }
    });
}

function quitChatRoom(RoomId) {
    var chatRoomId = RoomId; // 聊天室 Id。
    RongIMClient.getInstance().quitChatRoom(chatRoomId, {
        onSuccess: function() {
            console.log('退出聊天室成功')
            // 退出聊天室成功
        },
        onError: function(error) {
            // 退出聊天室失败
        }
    });
}

//创建URL地址
function creatUrl(param)
{
    var urlArray   = [];
    var _tempArray = param.split("/");
    for(var index in _tempArray)
    {
        if(_tempArray[index])
        {
            urlArray.push(_tempArray[index]);
        }
    }

    if(urlArray.length >= 2)
    {
        var iwebshopUrl = _webUrl.replace("_controller_",urlArray[0]).replace("_action_",urlArray[1]);

        //存在URL参数情况
        if(urlArray.length >= 3)
        {
            //卸载原数组中已经拼接的数据
            urlArray.splice(0,2);

            //伪静态格式
            if(iwebshopUrl.indexOf("?") == -1)
            {
                var param1 = [];
                var param2 = "";
                for(var t in urlArray)
                {
                    if(urlArray[t].indexOf('&') == -1 || urlArray[t].indexOf('=') == -1)
                    {
                        param1.push(urlArray[t]);
                    }
                    else
                    {
                        param2 += urlArray[t];
                    }
                }
                iwebshopUrl = iwebshopUrl.replace("_paramKey_/_paramVal_",param1.join("/"));
                iwebshopUrl+= param2 ? "?"+param2 : "";
            }
            //非伪静态格式
            else
            {
                var _paramVal_ = "";
                for(var i in urlArray)
                {
                    if(i%2 == 0)
                    {
                        _paramVal_ += urlArray[i];
                    }
                    else
                    {
                        _paramVal_ += "="+urlArray[i];

                        //网址末尾不需要加&连接符号
                        if(i != urlArray.length-1)
                        {
                            _paramVal_ += "&";
                        }
                    }
                }
                iwebshopUrl = iwebshopUrl.replace("_paramKey_=_paramVal_",_paramVal_);
            }
        }
        return iwebshopUrl;
    }
    return '';
}

